package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Notice;
import cn.wolfcode.crm.service.INoticeService;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.util.EmployeeUtil;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiresPermissions(value = {"notice:list", "公告通知页面"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("result", pageInfo);
        return "notice/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"notice:delete", "公告通知删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult(true, "删除成功");
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"notice:saveOrUpdate", "公告通知新增/编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice) {
        notice.setUer(EmployeeUtil.getEmployee());
        notice.setPubdate(new Date());
        if (notice.getId() != null) {
            noticeService.update(notice);
        } else {
            noticeService.save(notice);
        }
        return new JsonResult(true, "操作成功");
    }
    @RequestMapping("/show")
    @RequiresPermissions(value = {"notice:show", "公告通知查看"}, logical = Logical.OR)
    //@ResponseBody
    public String show(Model model,Long id) {
        Notice notice = noticeService.get(id);
        model.addAttribute("notice",notice);
        Notice notice1 = noticeService.selectByNoticeId(EmployeeUtil.getEmployee().getId(), id);
        System.out.println(EmployeeUtil.getEmployee().getId());
        if (notice1 == null) {
            noticeService.insertByEmployeeId(EmployeeUtil.getEmployee().getId(), id);
        }
        return "notice/show";
    }
}
